import modelExtend from 'dva-model-extend'
import { model } from 'models/common'
import { message } from 'antd'
import { AddNotice, DeleteNotice, queryPage, findNoticeDetail, updateNoticeDetail} from 'services/noticeManage'

export default modelExtend(model, {
    namespace:'noticeManage',
    state:{
        deleteId:[],
    },
    subscriptions: {
        setup({ dispatch, history }) {
            history.listen((location) => {
                if (location.pathname.includes('noticeManage')) {
                    dispatch({
                        type:'queryPage',
                        payload:{
                            size: 10,
                            start: 1
                        }
                    })
                } else if (location.pathname.includes('userNotice')){
                    dispatch({
                        type: 'queryPage',
                        payload: {
                            size: 10,
                            start: 1
                        }
                    })
                }
            })
        },
    },
    effects:{
        *queryPage({payload},{call,put}){
            const data = yield call(queryPage,payload);
            if(data.code === '200'){
                const noticeList = data.data.page;
                const allCount = data.data.allcount;
                yield put({
                    type:'updateState',
                    payload:noticeList,
                });
                yield put({
                    type:'updateAllCount',
                    payload:allCount,
                })
            }
        },
        *AddNotice({payload},{call,put}){
            const res = yield call(AddNotice,payload);
            if(res.code === '200'){
                yield put({
                    type:'queryPage',
                    payload:{
                        size: 10,
                        start: 1
                    }
                })
                message.success('添加通知成功！',3);
            }else{
                message.error(res.msg);
            }
        },
        *DeleteNotice({payload},{call,put}){
            const data = yield call(DeleteNotice,payload);
            if(data.code === '200'){
                yield put({
                    type:'queryPage',
                    payload:{
                        size: 10,
                        start: 1
                    }
                })
                message.success('删除通知成功',3);
            }else{
                message.error(data.msg)
            }
        },
        *findNoticeDetail({payload},{call,put}){
            const data = yield call(findNoticeDetail,payload);
            if(data.code === '200'){
                const dataList = data.data;
                yield put({
                    type:'updateDetail',
                    payload:dataList
                })
            }else{
                message.error(data.msg)
            }
        },
        *updateNoticeDetail({payload},{call,put}){
            const data = yield call(updateNoticeDetail,payload);
            if(data.code === '200'){
                yield put({
                    type:'queryPage',
                    payload:{
                        size:10,
                        start:1
                    }
                })
                message.success('修改通知成功！',3);
            }else{
                message.error(data.msg)
            }
        }
    },
    reducers:{
        updateState(state,{payload}){
            return{
                ...state,
                noticeList:payload
            }
        },
        updateAllCount(state,{payload}){
            return{
                ...state,
                allCount:payload
            }
        },
        updateDelete(state,{payload}){
            return{
                ...state,
                deleteId: payload
            }
        },
        updateDetail(state,{payload}){
            return {
                ...state,
                dataList:payload
            }
        }
    }
})